Python 常用套路总结_1

0x00 前言

跟着“菜鸟学Python”这个我最喜欢的公众号文章做点练习和巩固基本套路

0x01 字符串(string)的使用

  • 主要复习了字符串分割,字符串列表按长度排序的方法
text1='A dict that contains arbitrary metadata for this request. ' \
'This dict is empty for new Requests, ' \
'and is usually populated by different Scrapy components . ' \
'So the data contained in this dict depends on the extensions you have enabled.'


text2 = text1.split(' ') #分割字符串,得到下面的由字符串组成的列表

#['A', 'dict', 'that', 'contains', 'arbitrary', 'metadata', 'for', 'this', 'request.', 'This', 'dict', 'is', 'empty', 'for', 'new', 'Requests,', 'and', 'is', 'usually', 'populated', 'by', 'different', 'Scrapy', 'components', '.', 'So', 'the', 'data', 'contained', 'in', 'this', 'dict', 'depends', 'on', 'the', 'extensions', 'you', 'have', 'enabled.']

text3 = sorted(text2,key = lambda x:len(x),reverse=True) #按分割后的每个字符串长度排序

print(text3[0]) #得到最长的那个字符串
  • 找单词的长度是5或6,主要复习了列表生成式
list = [x for x in text2 if 6>=len(x)>=5] #利用列表生成式

print(list)
  • 统计每个字符串出现的次数,主要复习了Counter类的使用,常用于统计
from collections import Counter #统计每个字符串的出现次数

result = dict(Counter(text2))

#output:输出字典形式的统计结果
{'empty': 1, 'metadata': 1, 'Scrapy': 1, 'by': 1, 'dict': 3, 'the': 2, 'contains': 1, 'enabled.': 1, 'So': 1, 'contained': 1, '.': 1, 'is': 2, 'in': 1, 'new': 1, 'that': 1, 'components': 1, 'Requests,': 1, 'different': 1, 'This': 1, 'depends': 1, 'extensions': 1, 'on': 1, 'and': 1, 'you': 1, 'this': 2, 'request.': 1, 'arbitrary': 1, 'for': 2, 'have': 1, 'populated': 1, 'A': 1, 'usually': 1, 'data': 1}
  • 字符串的拼接, join()函数和字符串拼接符“+”的运用和对比,建议大型拼接用join()

names=['Hello',' James',',',' how',' are',' you', '!']

print(''.join(names))

---------------------------
s=''
for i in names:
s=s+i
print(s)

0x02 字典(dict)的使用

  • 通过key,value排序,这里需要借助 dict.items() 方法以列表方式返回键值对

print(result.items())#得到由元组组成的列表

dict_items([('empty', 1), ('metadata', 1), ('Scrapy', 1), ('by', 1), ('dict', 3), ('the', 2), ('contains', 1), ('enabled.', 1), ('So', 1), ('contained', 1), ('.', 1), ('is', 2), ('in', 1), ('new', 1), ('that', 1), ('components', 1), ('Requests,', 1), ('different', 1), ('This', 1), ('depends', 1), ('extensions', 1), ('on', 1), ('and', 1), ('you', 1), ('this', 2), ('request.', 1), ('arbitrary', 1), ('for', 2), ('have', 1), ('populated', 1), ('A', 1), ('usually', 1), ('data', 1)])


print(sorted(result.items(),key = lambda x:x[0],reverse=True)) #以字典的key排序

print(sorted(result.items(),key = lambda x:x[1],reverse=True)) #以字典的value排序
  • 有序字典OrderedDict(),按照插入顺序进行输出
from collections import OrderedDict

orderdict = OrderedDict() #按输入的顺序有序输出

orderdict['a'] = 1
orderdict['b'] = 2
orderdict['c'] = 3
orderdict['d'] = 4

print(orderdict)

dict = {} #普通字典不能按输入的顺序输出,无序输出

dict['a'] = 1
dict['b'] = 2
dict['c'] = 3
dict['d'] = 4

print(dict)
  • 字典的取值,建议用get()方法代替传统方法,增加代码健壮性

传统的取值dict[key],当key不是字典dict的键,会引起异常,但get()当key不存在时会返回空,不会导致程序异常

dict = {}

dict['a'] = 1
dict['b'] = 2
dict['c'] = 3
dict['d'] = 4

print(dict('e')) #报错,程序终止

print(dict.get('e')) #返回none,程序继续运行

print(dict.get('e','not found')) #自定义异常信息,返回not found,程序继续运行

0x03 Python操作集合的三架马车(filter、map/reduce、sorted)+zip()

  • map()/reduce()

map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回。

def f(x):
return x * x
r = map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9])

print(r) #函数返回一个map object,通过遍历得到每个值

for item in r:
print(item)
print(type(item))

print(list(map(f,[1, 2, 3, 4, 5, 6, 7, 8, 9])))# 一句话解决问题

#output
<map object at 0x10e7d2d68>
1
<class 'int'>
4
<class 'int'>

.......

[1, 4, 9, 16, 25, 36, 49, 64, 81]

reduce()把一个函数作用在一个序列[x1, x2, x3, …]上,这个函数必须接收两个参数,reduce把结果继续和序列的下一个元素做累积计算

  • filter()

和map()类似,filter()也接收一个函数和一个序列。和map()不同的是,filter()把传入的函数依次作用于每个元素,然后根据返回值是True还是False决定保留还是丢弃该元素。

def not_empty(s):
return s and s.strip()

print(filter(not_empty, ['A', '', 'B', None, 'C', ' ']))

print(list(filter(not_empty, ['A', '', 'B', None, 'C', ' '])))

#output

<filter object at 0x100da1cf8> #返回一个惰性序列

['A', 'B', 'C']#用list()函数遍历求解
  • sorted()

sorted()函数就可以对list进行排序,此外,sorted()函数也是一个高阶函数,它还可以接收一个key函数来实现自定义的排序


L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]

print(sorted(L,key=lambda x:x[1],reverse=True)) #对数字又高到低排列

print(sorted(L,key=lambda x:str(x[0].lower))) #对名字进行大小写不敏感排列
  • zip()

zip()是 Python 的一个内建函数,它接受一系列可迭代的对象作为参数,将对象中对应的元素打包成一个个tuple(元组),然后返回由这些tuples组成的list(列表)。若传入参数的长度不等,则返回 list 的长度和参数中长度最短的对象相同。利用*号操作符,可以将list unzip(解压)。

l1 = [1, 2, 3, 4]
l2 = ['a', 'b', 'c', 'd']

l4 = zip(l1,l2)

print(l4)#输出惰性序列

l4 = tuple(zip(l1,l2)) #使用tuple()函数遍历得到元组

print(l4)

l5 = dict(l4) #转化为字典

print(l5)

#output

<zip object at 0x1078e0848>
((1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'))
{1: 'a', 2: 'b', 3: 'c', 4: 'd'}

0x04 枚举(enumerate) – Python内置函数

常常会在别人的代码中看到它的身影,它允许我们遍历数据并自动计数

my_list = ['apple', 'banana', 'grapes', 'pear']
for c, value in enumerate(my_list,1):
print(c, value)

print(enumerate(my_list,1))

print(dict(enumerate(my_list,1)))
print(list(enumerate(my_list,1)))
print(tuple(enumerate(my_list,1)))

#output:

1 apple
2 banana
3 grapes
4 pear
<enumerate object at 0x103d10828> #输出惰性序列
{1: 'apple', 2: 'banana', 3: 'grapes', 4: 'pear'}
[(1, 'apple'), (2, 'banana'), (3, 'grapes'), (4, 'pear')]
((1, 'apple'), (2, 'banana'), (3, 'grapes'), (4, 'pear'))